﻿using Mario.GameEngine;
using Mario.Properties;
using Mario.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mario
{
    public partial class PreviewWindow : Form
    {
        private bool goRight = false;
        private bool goLeft = false;
        private bool goUp = true;
        private bool goDown = false;
        private int jumpHeight = 21;
        private int force;
        private int groundHeight = 100;
        private PictureBox player = new PictureBox();
        private bool shouldClose = true;

        public PreviewWindow()
        {
            InitializeComponent();
            DoubleBuffered = true;
            Pipe1.Parent = BackGround;
            Pipe2.Parent = BackGround;
            Pipe3.Parent = BackGround;
            Pipe_Rot.Parent = BackGround;
            World1.Parent = BackGround;
            World2.Parent = BackGround;
            World3.Parent = BackGround;
        }

        /// <summary>
        /// Method on form closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PreviewWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (shouldClose)
            {
                foreach (Form form in Application.OpenForms)
                {
                    if (form is MainWindow1)
                    {
                        form.Show();
                        break;
                    }
                }
            }
        }

      
        /// <summary>
        /// Timer tick action
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (goRight)
            {

                if (((player.Location.X + player.Size.Width) <= Pipe1.Location.X) || ((player.Location.X >= (Pipe1.Location.X + Pipe1.Size.Width)) && ((player.Location.X + player.Size.Width) <= Pipe2.Location.X))
                    || ((player.Location.X >= (Pipe2.Location.X + Pipe2.Size.Width)) && ((player.Location.X + player.Size.Width) <= Pipe3.Location.X))
                    || ((player.Location.X >= (Pipe3.Location.X + Pipe3.Size.Width))))
               {
                    player.Location = new Point(player.Location.X + 10, player.Location.Y);
               }
               else
               {
                    if ((player.Location.Y + player.Size.Height) <= Pipe1.Location.Y)
                    {
                        player.Location = new Point(player.Location.X + 10, player.Location.Y);
                        goUp = true;
                    }
               }
               if (((player.Location.X + player.Size.Width) > Pipe1.Location.X) && (player.Location.X < (Pipe1.Location.X + Pipe1.Size.Width))
                     && ((player.Location.Y + player.Size.Height) > Pipe1.Location.Y))
                {
                    player.Location = new Point(Pipe1.Location.X - player.Size.Width, player.Location.Y);
                }
                if (((player.Location.X + player.Size.Width) > Pipe2.Location.X) && (player.Location.X < (Pipe2.Location.X + Pipe2.Size.Width))
                  && ((player.Location.Y + player.Size.Height) > Pipe1.Location.Y))
                {
                    player.Location = new Point(Pipe2.Location.X - player.Size.Width, player.Location.Y);
                }
                if (((player.Location.X + player.Size.Width) > Pipe3.Location.X) && (player.Location.X < (Pipe3.Location.X + Pipe3.Size.Width))
                  && ((player.Location.Y + player.Size.Height) > Pipe1.Location.Y))
                {
                    player.Location = new Point(Pipe3.Location.X - player.Size.Width, player.Location.Y);
                }

               if ((player.Location.X + player.Size.Width) >= this.Size.Width)
               {
                   player.Location = new Point(this.Size.Width - player.Size.Width, player.Location.Y);
               }
            }
            if (goLeft)
            {
               // player.Location = new Point(player.Location.X - 9, player.Location.Y);

                if (((player.Location.X + player.Size.Width) <= Pipe1.Location.X) || ((player.Location.X >= (Pipe1.Location.X + Pipe1.Size.Width)) && ((player.Location.X + player.Size.Width) <= Pipe2.Location.X))
                   || ((player.Location.X >= (Pipe2.Location.X + Pipe2.Size.Width)) && ((player.Location.X + player.Size.Width) <= Pipe3.Location.X))
                   || ((player.Location.X >= (Pipe3.Location.X + Pipe3.Size.Width))))
                {
                    player.Location = new Point(player.Location.X - 10, player.Location.Y);
                }
                else
                {
                    if ((player.Location.Y + player.Size.Height) <= Pipe1.Location.Y)
                    {
                        player.Location = new Point(player.Location.X - 10, player.Location.Y);
                        goUp = true;
                    }
                }
                if (((player.Location.X < (Pipe1.Location.X + Pipe1.Size.Width)) && (player.Location.X > Pipe1.Location.X))
                    && ((player.Location.Y + player.Size.Height) > Pipe1.Location.Y))
                {
                    player.Location = new Point(Pipe1.Location.X + Pipe1.Size.Width, player.Location.Y);
                }
                if (((player.Location.X < (Pipe2.Location.X + Pipe2.Size.Width)) && (player.Location.X > Pipe2.Location.X))
                     && ((player.Location.Y + player.Size.Height) > Pipe1.Location.Y))
                {
                    player.Location = new Point(Pipe2.Location.X + Pipe2.Size.Width, player.Location.Y);
                }
                if (((player.Location.X < (Pipe3.Location.X + Pipe3.Size.Width)) && (player.Location.X > Pipe3.Location.X))
                     && ((player.Location.Y + player.Size.Height) > Pipe1.Location.Y))
                {
                    player.Location = new Point(Pipe3.Location.X + Pipe3.Size.Width, player.Location.Y);
                }
                if (player.Location.X < 0)
                {
                    player.Location  = new Point(0, player.Location.Y);
                    
                }
                
            }
            if (goUp)
            {
                player.Location = new Point(player.Location.X, player.Location.Y - force);
                force--;
            }
            if (((player.Location.X + player.Size.Width) > Pipe1.Location.X) && ((Pipe1.Location.X + Pipe1.Size.Width) > player.Location.X) 
                && (player.Location.Y + player.Size.Height > Pipe1.Location.Y))
            {
                goUp = false;
                player.Location = new Point(player.Location.X, Pipe1.Location.Y - player.Size.Height);
            }
            if (((player.Location.X + player.Size.Width) > Pipe2.Location.X) && ((Pipe2.Location.X + Pipe2.Size.Width) > player.Location.X)
               && (player.Location.Y + player.Size.Height > Pipe2.Location.Y))
            {
                goUp = false;
                player.Location = new Point(player.Location.X, Pipe1.Location.Y - player.Size.Height);
            }
            if (((player.Location.X + player.Size.Width) > Pipe3.Location.X) && ((Pipe3.Location.X + Pipe3.Size.Width) > player.Location.X)
               && (player.Location.Y + player.Size.Height > Pipe3.Location.Y))
            {
                goUp = false;
                player.Location = new Point(player.Location.X, Pipe1.Location.Y - player.Size.Height);
            }
            if (player.Location.Y + player.Size.Height > this.Height - groundHeight)
            {
                goUp = false;
                player.Location = new Point(player.Location.X, this.Height - groundHeight - player.Size.Height);
            }

            if (goDown)
            {
                if ((player.Location.X >= Pipe1.Location.X) && ((player.Location.X + player.Size.Width) < (Pipe1.Location.X + Pipe1.Size.Width)))
                {
                    GameWindow startGame = new GameWindow("World1");
                    startGame.Show();
                    timer1.Stop();
                    goDown = false;
                    shouldClose = false;
                    this.Close();
                }
                if ((player.Location.X >= Pipe2.Location.X) && ((player.Location.X + player.Size.Width) < (Pipe2.Location.X + Pipe3.Size.Width)))
                {
                    GameWindow startGame = new GameWindow("World1");
                    startGame.Show();
                    goDown = false;
                    timer1.Stop();
                    shouldClose = false;
                    this.Close();
                }
                if ((player.Location.X >= Pipe2.Location.X) && ((player.Location.X + player.Size.Width) < (Pipe3.Location.X + Pipe3.Size.Width)))
                {
                    GameWindow startGame = new GameWindow("World1");
                    startGame.Show();
                    goDown = false;
                    timer1.Stop();
                    shouldClose = false;
                    this.Close();
                }
            }
        }

        /// <summary>
        /// Key Down method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PreviewWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (!goUp)
            {
                if (e.KeyCode == Keys.W)
                {
                    goUp = true;
                    force = jumpHeight;
                }
            }
            if (e.KeyCode == Keys.A)
            {
                goLeft = true;
            }
            if (e.KeyCode == Keys.D)
            {
                goRight = true;
            }
            if (e.KeyCode == Keys.S)
            {
                goDown = true;
            }

        }

        /// <summary>
        /// Key up method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PreviewWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A)
            {
                goLeft = false;
            }
            if (e.KeyCode == Keys.D)
            {
                goRight = false;
            }
            if (e.KeyCode == Keys.S)
            {
                goDown = false;
            }
        }

        /// <summary>
        /// On load method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PreviewWindow_Load(object sender, EventArgs e)
        {
            timer1.Start();
            this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = System.Drawing.Color.Transparent;
            object O = Resources.Resource1.Small_mario;
            player.BackColor = Color.Transparent;
            player.Image = (Image)O;
            player.Location = new Point(0, 200);
            player.Size = new Size(50, 50);
            BackGround.Controls.Add(player);
            player.BringToFront();
        }


    }
}
